package cn.pn.project.controller;

import cn.pn.project.client.base.CouponFeign;
import cn.pn.project.client.commodity.CommodityFeign;
import cn.pn.project.pojo.base.Coupon;
import cn.pn.project.pojo.commodity.Commodity;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/Coupon")
public class CouponController {
    @Autowired
    private CouponFeign couponFeign;
    @Autowired
    private CommodityFeign commodityFeign;

    /**
     * 获取商品可用的所有优惠券
     * @return
     */
    @RequestMapping(value = "/v/findCoupon")
    @ResponseBody
    public List<Coupon> findCoupon(@RequestParam(value = "commodityId",required = false) Integer commodityId) throws Exception{
        //通过商品id获取到商品的所有信息
        Commodity commodity= commodityFeign.findCommodityAllMessage(commodityId);
        System.out.println(commodity.getCommodityId()+"ccc****************************************************"+commodity.getCommodityName());
        List<Coupon> coupons = couponFeign.findAllByCouponTypeAndReleId(commodity.getClildTypeId(),commodity.getCommodityId());
        System.out.println(JSON.toJSON(coupons));
        return coupons;
    }

    /**
     * 获取所有未过期的优惠券
     * @return
     */
    @RequestMapping(value = "/v/findAllCoupon")
    @ResponseBody
    public List<Coupon> findAllCoupon()throws Exception{
        List<Coupon> couponList=couponFeign.findAllCoupon();
        System.out.println(JSON.toJSON(couponList));
        return couponList;
    }
}
